import 'package:gold/app/user_provider.dart';
import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_my.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/point_list_model.dart';
import 'package:gold/model/point_statistics_model.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description:
/// @author tangzhentao
/// @created at 21:32 2024/12/10

class IntegralDetailViewmodel extends Viewmodel {
  /// 积分统计
  PointStatisticsModel statistics = PointStatisticsModel();
  /// 数据
  final List<PointModel> list = [];
  /// 刷新控制器
  final RefreshController controller = RefreshController();
  /// 页码
  int _page = 1;

  /// 加载
  Future loadFirst() async {
    setLoading();
    _page = 1;
    final pointResult = await Http().request<PointStatisticsModel>(ApiMy.logsPointStatistics, showErrorMsg: false);
    final result = await Http().request<PointListModel>(ApiMy.logsPoint, showErrorMsg: false, data: {'page': _page});
    if (pointResult.success && result.success) {
      if (pointResult.data != null) {
        statistics = pointResult.data!;
        if (statistics.remainingPoints != null) {
          XfgNav.read<UserProvider>().updatePoint(statistics.remainingPoints!);
        } else {
          statistics.remainingPoints = XfgNav.read<UserProvider>().user.points;
        }
      }
      list.clear();
      list.addAll(result.data?.data?? []);
      if (list.isNotEmpty) {
        setSuccess();
        controller.headerMode?.value = RefreshStatus.canRefresh;
        controller.footerMode?.value = _page < (result.data?.totalPages?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      } else {
        setEmpty();
      }
    } else {
      String msg = '';
      if (!pointResult.success) {
        msg = pointResult.msg;
      }
      if (!result.success) {
        msg = result.msg;
      }
      setFailed(msg: msg);
    }
  }

  /// 刷新数据
  Future refresh() async {
    _page = 1;
    final result = await Http().request<PointListModel>(ApiMy.logsPoint, showErrorMsg: false, data: {'page': _page});
    if (result.success) {
      controller.refreshCompleted();
      controller.footerMode?.value = _page < (result.data?.totalPages?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.data?? [];
      if (data.isNotEmpty) {
        list.clear();
        list.addAll(data);
        setSuccessChange();
      }
    } else {
      controller.refreshFailed();
    }
  }

  /// 加载更多数据
  Future loadMore() async {
    _page++;
    final result = await Http().request<PointListModel>(ApiMy.logsPoint, showErrorMsg: false, data: {'page': _page});
    if (result.success) {
      controller.loadComplete();
      controller.footerMode?.value = _page < (result.data?.totalPages?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.data?? [];
      if (data.isNotEmpty) {
        list.addAll(data);
        setSuccessChange();
      } else {
        controller.loadNoData();
      }
    } else {
      controller.loadFailed();
    }
  }

  @override
  onReload() => loadFirst();

  @override
  onRetry() => loadFirst();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
